<?php
/**
* Subsystem for logging
* @author trungdt
* @since Apr, 2012
* @copyright ABSoft Viet Nam
* @version 1.0
*/


define("SYS_LOG",1);
define("E_EXPONENT",0);   

function exponent_syslog($error_code, $errstr, $errfile = "", $errline="", $module_data = "")
{
	global $browser;
    global $db;
    if ($db == null)
        return;
	
	if (!isset($browser))
		$browser=new Browser();
	
	// build client info
	$client = null;
	$client->is_mobile = $browser->isMobile();
	$client->browser = $browser->getBrowser();
	$client->version = $browser->getVersion();
	$client->user_agent = $browser->getUserAgent();
	$client->platform = $browser->getPlatform();
	
	$error=null;
	$error->error_type=$error_code;
	$error->error_str= $errstr;
	$error->error_str= $errstr;
	$error->error_file= $errfile;
	$error->error_line= $errline;
	$error->error_date= time();
	$error->in_dev = DEVELOPMENT;
	$error->dump_request = json_encode($_REQUEST);
	$error->dump_server = json_encode($_SERVER);
	$error->client_info = json_encode($client);
	$error->module_data = $module_data;
	$db->insertObject($error,"syslog");
	
}

function handleErrors($errno, $errstr, $errfile, $errline) {
	/*if (DEVELOPMENT > 0) {
		$msg = "";
		switch ($errno) {
				case E_USER_ERROR:
					$msg = 'PHP Error('.$errno.'): ';
				break;
				case E_USER_WARNING:
					$msg = 'PHP Warning('.$errno.'): ';
				break;
				case E_USER_NOTICE:
				case E_NOTICE:
					$msg = 'PHP Notice('.$errno.'): ';
				default:
				$msg = 'PHP Issue('.$errno.'): ';
				break;    
			}
				$msg .= $errstr;
		$msg .= !empty($errfile) ? ' in file '.$errfile : "";
		$msg .= !empty($errline) ? ' on line '.$errline : "";
	}*/
	
	//if (DEVELOPMENT == 0 && $errno == E_NOTICE)
	//	return;
	if (in_array($errno, array(E_DEPRECATED, E_USER_DEPRECATED, E_USER_WARNING, E_USER_NOTICE, E_STRICT, E_NOTICE)))
		return;
	
	// check for if error from template compiled? inorge it
	$errfile = substr($errfile,strlen(BASE)+1);
	$errstr=str_replace(BASE,"", $errstr);
	if (substr($errfile,0,1) == "/")
	{
		$tmp_path = "tmp/views_c";
		if (strpos($errfile, $tmp_path)>0)
		return;
	}
	else
	{
		$tmp_path = "tmp\\views_c"; 
		if (strpos($errfile, $tmp_path)>0)
		return;
	}
	exponent_syslog($errno,$errstr, $errfile, $errline,"");
}

error_reporting ((E_ERROR | E_WARNING | E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR | E_COMPILE_WARNING) & ~E_STRICT);
set_error_handler("handleErrors");

?>
